﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using m3u;

namespace Voize
{
    internal class CStartupHandler
    {


        internal CStartupHandler(CSettings settings)
        {
            m_Settings = settings;
        }



        private CSettings m_Settings;



        internal bool AskPlaylistSavingOnClose()
        {
            if (m_Settings.AskPlaylistSavingOnClose == "true")
                return true;
            else
                return false;
        }



        internal bool RememberWindowSizeAndLocation()
        {
            if (m_Settings.RememberWindowSizeAndLocation == "true")
                return true;
            else
                return false;
        }



        internal bool RememberLastSessionOnStartup()
        {
            if (m_Settings.RememberLastSessionOnStartup == "true")
                return true;
            else
                return false;
        }



        internal bool MinimizeToSystemTrayOnClose()
        {
            if (m_Settings.MinimizeToSystemTrayOnClose == "true")
                return true;
            else
                return false;
        }



        internal string GetLastSessionPlaylistName()
        {
            string UserAppData = Environment.GetEnvironmentVariable("APPDATA");
            string LastSessionPlaylist = "LastSessionPlaylist.voize";

            UserAppData += @"\Voize\";

            if (!Directory.Exists(UserAppData))
                Directory.CreateDirectory(UserAppData);

            return UserAppData + LastSessionPlaylist;
        }



        internal void SaveLastSessionPlayList(List<string> lPlaylist)
        {           
            //StreamWriter writer;

            //writer = new StreamWriter(this.GetLastSessionPlaylistName());
            //writer.WriteLine("#Generated by Voize " + DateTime.Now.ToString());

            //foreach (string song in lPlaylist)
            //    writer.WriteLine(song);

            //writer.Close();

            M3UFactory factory = new M3UFactory();
            IWriter writer = factory.CreateWriter();
            string filename = this.GetLastSessionPlaylistName();

            if (filename.EndsWith(".m3u"))
                writer.SaveM3U(lPlaylist, filename);
            else
            {

                writer.Save(lPlaylist, filename);
            }
        }


    }
}
